package com.voltmao.controller.module.corp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.service.application.corp.CorpOrganizationServiceImpl;
import com.voltmao.service.application.corp.EmployesUserServiceImpl;
import com.voltmao.service.application.module.SysUserInfoServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/module/corp/empl_info")
public class EmployesUserController {

    @Autowired
    EmployesUserServiceImpl employesUserService;

    @Autowired
    CorpOrganizationServiceImpl corpOrganizationService;
    @Autowired
    SysUserInfoServiceImpl sysUserInfoService;

    /**
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}.json")
    @ResponseBody
    public ApiResult getById(HttpServletRequest request,
                             @PathVariable(value = "id") String id){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                return employesUserService.selectById(id);
            }
        });
    }

    /**
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUser.json")
    @ResponseBody
    public ApiResult getByProviderId(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return employesUserService.getUserByProviderId(params.get("providerId")+"");
            }
        });
    }
    /**
     * 更新或新增员工信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/insertOrUpdate.json")
    @ResponseBody
    public ApiResult UpdateEnable(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);

                Map<String, Object> resultMap = new HashMap<String, Object>();
                try{
                    int flag = employesUserService.insertOrUpdate(params);
                }catch (Exception e){
                    resultMap.put("error",e.getMessage());
                }

                return resultMap;
            }
        });
    }

    /**
     * 根据部门查询条件获取员工表格列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/loadSearView.json")
    @ResponseBody
    public GridViewResult loadSearView(HttpServletRequest request){
        return new DTOSmith(request).builderSeachGridResult(new DTOSmith.SeachGridBackCall() {
            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                apiResult.dataUrl = "/loadSearView.json";
                return employesUserService.getEmployerPageData(params);
            }
        });
    }

    /**
     * 初始化数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/loadGridView.json")
    @ResponseBody
    public GridViewResult LoadGridView(HttpServletRequest request){
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/module/corp/empl_info/loadSearView.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return employesUserService.getEmployerPageData(params);
            }

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                Map<String, Object> defalutMap = new HashMap<String, Object>();
                defalutMap.put("orgs",corpOrganizationService.selectAllOrg());

                return  defalutMap;
            }
        });
    }

    /**
     * 获取商务部人员
     * @param request
     * @return
     */
    @RequestMapping(value = "/businessPersonnel.json")
    @ResponseBody
    public ApiResult getBusinessPersonnel(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                return employesUserService.getBusinessPersonnel();
            }
        });
    }


}
